// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper } = pathImport;;
const { createStoreBindings } = require('mobx-miniprogram-bindings');
const { sampleStore } = require('../../../config/store_config.js');

// 调试模式开关
const DEBUG_MODE = true;

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		// 仅保留UI配置相关的本地状态
		// 业务数据从store中获取
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);
		
		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: sampleStore,
			fields: [
				'sampleList',
				'filter',
				'pagination',
				'loading',
				'canAdd'
			],
			actions: [
				'setSearchKey',
				'setStatusFilter',
				'setSort',
				'resetPagination',
				'loadSampleList',
				'checkAddPermission',
				'handleImageError'
			]
		});
		
		// 初始化数据
		this.resetPagination();
		await this.loadSampleList();
		await this.checkAddPermission();
	},
	
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		// 销毁绑定
		if (this.storeBindings) {
			this.storeBindings.destroyStoreBindings();
		}
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		this.resetPagination();
		await this.loadSampleList();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: async function () {
		await this.loadSampleList();
	},

	/**
	 * 搜索输入
	 */
	bindSearchInput: function (e) {
		this.setSearchKey(e.detail.value);
	},

	/**
	 * 搜索点击
	 */
	bindSearchTap: async function (e) {
		this.resetPagination();
		await this.loadSampleList();
	},

	/**
	 * 图片加载错误处理
	 */
	onImageError: function(e) {
		const index = e.currentTarget.dataset.index;
		this.handleImageError(index);
	},

	url: async function (e) {
		pageHelper.url(e, this);
	}
}); 